﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Web.Script.Services;
using System.Web.Services;
using DevExpress.Web.ASPxEditors;
using System.Data.SqlClient;
using DevExpress.Utils;

public partial class BasicInfo_SupplierEdit : Frame.Pages.ActionInfomationEditBasePage
{
    protected override string ActionID
    {
        get { return "2003"; }
    }

    protected override string UserFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return "IUDQ";
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? CMSUser.GetFunction(ActionID).op : "";
                }
            }
            else
            {
                return "";
            }
        }
    }

    protected override bool IsShowSpecialFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return true;
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? (CMSUser.GetFunction(ActionID).op_s == "Y" ? true : false) : false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    protected override void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Frame.Global.GlobalFunctions.SetGridView(UserList, false);
            UserList.Styles.AlternatingRow.Enabled = DefaultBoolean.False;
        }
        base.Page_Load(sender, e);
    }

    protected void gridView_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        #region 删除选中行
        //List<string> IDlist = gridView.GetSelectedFieldValues("csbm").Select<object, string>(o => o.ToString()).ToList();
        //using (DataClassesDataContext context = new DataClassesDataContext())
        //{
        //    foreach (string item in IDlist)
        //    {
        //        css_csdj model = context.css_csdj.FirstOrDefault(o => o.csbm.Trim() == item.Trim());
        //        context.css_csdj.DeleteOnSubmit(model);
        //    }
        //    context.SubmitChanges();
        //}
        //this.gridView.DataBind();
        #endregion
    }

    protected void gridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        e.NewValues["founder"] = CMSUser.Current.ID;
        e.NewValues["fdate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        e.NewValues["editor"] = CMSUser.Current.ID;
        e.NewValues["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }
    protected void gridView_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        e.NewValues["editor"] = CMSUser.Current.ID;
        e.NewValues["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }

    protected void gridView_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "csbm")
        {
            if (e.Value == null)
            {
                using (DAL.DataClassesDataContext db = new DataClassesDataContext())
                {
                    int number = db.css_csdj.Count() + 1;
                    (e.Editor as ASPxTextBox).Text = WebUtility.GenerateCode(number.ToString(), 'G', 10);
                }
            }
        }
    }

    protected void UserList_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Parameters))
        {
            Frame.Global.GlobalSqlHelper.ExecuteSQL("Delete css_csdj_rybm where csbm='" + e.Parameters + "'");

            List<string> IDlist = UserList.GetSelectedFieldValues("rybm").Select<object, string>(o => o.ToString()).ToList();
            using (DataClassesDataContext context = new DataClassesDataContext())
            {
                foreach (string item in IDlist)
                {
                    DAL.css_csdj_rybm cr = new DAL.css_csdj_rybm();
                    cr.csbm = e.Parameters;
                    cr.rybm = item.Trim();
                    context.css_csdj_rybm.InsertOnSubmit(cr);

                }
                context.SubmitChanges();
            }
        }
    }

    protected void ContractPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (!string.IsNullOrEmpty(e.Parameter))
        {
            using (DAL.DataClassesDataContext Dcontext = new DAL.DataClassesDataContext())
            {
                DAL.css_contract contract = Dcontext.css_contract.FirstOrDefault(o => o.htbm == e.Parameter);
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                if (contract != null)
                {
                    #region
                    sb.Append("<table width=\"100%\" style=\"font-size: 10pt\">");
                    sb.Append("<tr>");
                    sb.AppendFormat("<td width=\"80px\" style=\"white-space: nowrap\">{0}</td>", "结算方式：");
                    sb.AppendFormat("<td style=\"white-space: nowrap\">{0}</td>", contract.jsfs == "K" ? "扣率结算" : "固定场租");
                    sb.AppendFormat("<td width=\"80px\" style=\"white-space: nowrap\">{0}</td>", "结算扣率：");
                    sb.AppendFormat("<td style=\"white-space: nowrap\">{0}</td>", contract.jskl);
                    sb.Append("</tr>");
                    sb.Append("<tr>");
                    sb.AppendFormat("<td width=\"80px\" style=\"white-space: nowrap\">{0}</td>", "账期：");
                    sb.AppendFormat("<td style=\"white-space: nowrap\">{0}</td>", contract.zq);
                    sb.AppendFormat("<td width=\"80px\" style=\"white-space: nowrap\">{0}</td>", "保底金额：");
                    sb.AppendFormat("<td style=\"white-space: nowrap\">{0}</td>", contract.bdje);
                    sb.Append("</tr>");
                    sb.Append("</table>");
                    sb.Append("<table width=\"100%\" border=\"1px\" border=\"1\"  cellpadding=\"1\" cellspacing=\"0\" class=\"table2\">");
                    sb.Append("<tr class=\"head\">");
                    sb.AppendFormat("<td colspan=\"6\">{0}</td>", "合同费用明细");
                    sb.Append("</tr>");
                    sb.Append("<tr class=\"head\">");
                    sb.AppendFormat("<td width=\"100px\" >{0}</td>", "记录序号");
                    sb.AppendFormat("<td width=\"100px\" >{0}</td>", "费用名称");
                    sb.AppendFormat("<td width=\"100px\" >{0}</td>", "计费方式");
                    sb.AppendFormat("<td width=\"100px\" >{0}</td>", "费用金额");
                    sb.AppendFormat("<td width=\"100px\" >{0}</td>", "付款方式");
                    sb.AppendFormat("<td width=\"100px\" >{0}</td>", "扣率");
                    sb.Append("</tr>");
                    var fees = Dcontext.css_contract_fee.Where(f => f.htbm == contract.htbm);
                    foreach (var item in fees)
                    {
                        sb.Append("<tr>");
                        sb.AppendFormat("<td>{0}</td>", item.jlxh);
                        sb.AppendFormat("<td>{0}</td>", GetFymc(item.fybm));
                        sb.AppendFormat("<td>{0}</td>", GetFylx(item.fylx));
                        sb.AppendFormat("<td>{0}</td>", item.fyje);
                        sb.AppendFormat("<td>{0}</td>", GetFkfs(item.fkfs));
                        sb.AppendFormat("<td>{0}</td>", item.kl);
                        sb.Append("</tr>");
                    }
                    sb.Append("</table>");

                    sb.Append("<table width=\"100%\" border=\"1px\" border=\"1\"  cellpadding=\"1\" cellspacing=\"0\" class=\"table2\">");
                    sb.Append("<tr class=\"head\">");
                    sb.AppendFormat("<td colspan=\"6\">{0}</td>", "合同商品明细");
                    sb.Append("</tr>");
                    sb.Append("<tr class=\"head\">");
                    sb.AppendFormat("<td width=\"100px\" >{0}</td>", "记录序号");
                    sb.AppendFormat("<td width=\"100px\" >{0}</td>", "商品名称");
                    sb.AppendFormat("<td width=\"100px\" >{0}</td>", "扣率");
                    sb.Append("</tr>");
                    var sps = Dcontext.css_contract_sp.Where(s => s.htbm == contract.htbm);
                    foreach (var item in sps)
                    {
                        sb.Append("<tr>");
                        sb.AppendFormat("<td>{0}</td>", item.jlxh);
                        sb.AppendFormat("<td>{0}</td>", GetSpmc(item.spbm));
                        sb.AppendFormat("<td>{0}</td>", item.kl);
                        sb.Append("</tr>");
                    }
                    sb.Append("</table>");
                    #endregion
                }
                Detail.InnerHtml = sb.ToString();
            }
        }
    }

    string GetSpmc(string spbm)
    {
        using (DAL.DataClassesDataContext dc = new DataClassesDataContext())
        {
            DAL.css_spjbxx sp = dc.css_spjbxx.FirstOrDefault(s => s.spbm == spbm);
            if (sp != null)
            {
                return sp.spmc;
            }
            else
            {
                return spbm;
            }
        }
    }

    string GetFymc(string fybm)
    {
        using (DAL.DataClassesDataContext dc = new DataClassesDataContext())
        {
            DAL.css_fee fee = dc.css_fee.FirstOrDefault(f => f.fybm == fybm);
            if (fee != null)
            {
                return fee.fymc;
            }
            else
            {
                return fybm;
            }
        }
    }

    string GetFkfs(string fkfs)
    {
        using (DAL.DataClassesDataContext dc = new DataClassesDataContext())
        {
            DAL.css_fkfs fk = dc.css_fkfs.FirstOrDefault(k => k.fkfs == fkfs);
            if (fk != null)
            {
                return fk.fkmc;
            }
            else
            {
                return fkfs;
            }
        }
    }

    string GetFylx(string fylx)
    {
        switch (fylx)
        {
            case "G":
                return "固定费用";
            case "L":
                return "临时费用";
            case "X":
                return "按销售总金额浮动扣率";
            case "F":
                return "按付款方式销售金额浮动扣率";
            default:
                return "未知";
        }
    }

    [WebMethod]
    public static string[] GetUsers(string csbm)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var us = context.css_csdj_rybm.Where(u => u.csbm.Trim() == csbm.Trim()).Select(u => u.rybm).ToArray();
            return us;
        }
    }

    //[WebMethod]
    //public static void SaveUsers(string csbm)
    //{
    //    //if (!string.IsNullOrEmpty(csbm))
    //    //{
    //    //    Frame.Global.GlobalSqlHelper.ExecuteSQL("Delete css_csdj_rybm where csbm='" + csbm + "'");

    //    //    List<string> IDlist = UserList.GetSelectedFieldValues("rybm").Select<object, string>(o => o.ToString()).ToList();
    //    //    using (DataClassesDataContext context = new DataClassesDataContext())
    //    //    {
    //    //        foreach (string item in IDlist)
    //    //        {
    //    //            DAL.css_csdj_rybm cr = new DAL.css_csdj_rybm();
    //    //            cr.csbm = csbm;
    //    //            cr.rybm = item.Trim();
    //    //            context.css_csdj_rybm.InsertOnSubmit(cr);

    //    //        }
    //    //        context.SubmitChanges();
    //    //    }
    //    //}
    //}
}